import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/Home.vue'
import Member from '../components/Member.vue'
import Paper from '../components/Paper.vue'
import Edit from '../components/Edit.vue'
import Contact from '../components/Contact.vue'
import Album from '../components/Album.vue'
import store from '../store'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: Home, meta: { title: '空间电源实验室 - 首页' } },
  { path: '/member', component: Member, meta: { title: '空间电源实验室 - 成员' } },
  { path: '/paper', component: Paper, meta: { title: '空间电源实验室 - 论文' } },
  { path: '/edit', component: Edit, meta: { title: '空间电源实验室 - 编辑' } },
  { path: '/album', component: Album, meta: { title: '空间电源实验室 - 照片集' } },
  { path: '/contact', component: Contact, meta: { title: '空间电源实验室 - 联系方式' } }
]

const router = new VueRouter({
  routes: routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/edit') {
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/home')
  }
  store.commit('setActiveMenu', to.path.substr(1))
  document.title = to.meta.title
  next()
})

export default router
